// compile:
// run:
#include <iostream>
#include <memory>

using namespace std;

class Base {
 public:
  virtual ~Base() = default;

 private:
};
template <typename T>
class Derived : public Base {
 public:
  Derived(T data) : data_(data) {}
  ~Derived() = default;

 public:
  T data_;
};
class Any {
 public:
  Any() = default;
  ~Any() = default;
  Any(const Any&) = delete;
  Any& operator=(const Any&) = delete;
  Any(Any&&) = default;
  Any& operator=(Any&&) = default;

  template <typename T>
  Any(T data) : base_(std::make_unique<Derived<T>>(data)) {}
  template <typename T>
  T Any_Cast();

 public:
  std::unique_ptr<Base> base_;
};
template <typename T>
T Any::Any_Cast() {
  Derived<T>* ptr = dynamic_cast<Derived<T>*>(base_.get());
  if (ptr == nullptr) {
    std::cerr << "error: type is unmatched!" << std::endl;
    throw "type is unmatched!";
  }
  return ptr->data_;
}

void Test1() {
  Any a1 = 5;
  Any a2 = "leiyx";
  std::string s = "lei";
  Any a3 = s;
  Any a4 = 9.45;
  int a11 = a1.Any_Cast<int>();
  const char* a22 = a2.Any_Cast<const char*>();
  std::string a33 = a3.Any_Cast<std::string>();
  double a44 = a4.Any_Cast<double>();
  cout << a11 << endl;
  cout << a22 << endl;
  cout << a33 << endl;
  cout << a44 << endl;
  // int a444 = a4.Any_Cast<int>();  // 类型不兼容出错，抛出异常；
}
void Test2() {}
int main() {
  Test1();
  Test2();
  return 0;
}
